package com.kevin.Code.BinarySearch;

/**
 * @author Vinlee Xiao
 * @Classname FindMinimuminRotatedSortedArray
 * @Description Leetcode 153. 寻找旋转排序数组中的最小值 二分查找 中等难度 二刷思路更好
 * @Date 2022/2/26 14:59
 * @Version 1.0
 */
public class FindMinimuminRotatedSortedArray {

    public int findMin(int[] nums) {

        if (nums.length == 1) {
            return nums[0];
        }

        int low = 0;
        int high = nums.length - 1;

        while (low <= high) {
            int mid = (low + high) >> 1;

            if (nums[mid] <= nums[nums.length - 1]) {
                high = mid - 1;
            } else {
                low = mid + 1;
            }
        }

        return nums[low];
    }

    public static void main(String[] args) {
        int[] nums = new int[]{2, 1};
        FindMinimuminRotatedSortedArray findMinimuminRotatedSortedArray = new FindMinimuminRotatedSortedArray();
        int min = findMinimuminRotatedSortedArray.findMin(nums);
        System.out.println(min);
    }
}
